#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<string.h>
#define SIZE1 100
#define SIZE2 128
#define SIZE3 128
int main()
{
    while(1)
    {
        char env_bf[SIZE3];
        char Command_s[SIZE1];
        memset(Command_s,'\0',SIZE1);
        printf("[lrk@VM-8-13-centos dirname]$ ");
        fgets(Command_s,SIZE1,stdin);
        Command_s[strlen(Command_s)-1]='\0';


       char* Command_argv[SIZE2];
       Command_argv[0]=strtok(Command_s," ");
       int index=1;
       if(strcmp(Command_argv[0],"ls")==0)
       {
           Command_argv[index++]=(char*)"--color=auto";
       }
       if(strcmp(Command_argv[0],"ll")==0)
       {
           Command_argv[0]=(char*)"ls";
           Command_argv[index++]=(char*)"-l";
           Command_argv[index++]=(char*)"-color=auto";
       }
       while(Command_argv[index++]=strtok(NULL," "));
       if(strcmp(Command_argv[0],"export")==0)
       {
           strcpy(env_bf,Command_argv[1]);
           putenv(env_bf);
       }
       if(strcmp(Command_argv[0],"cd")==0&&Command_argv[1]!=NULL)
       {
            chdir(Command_argv[1]);
            continue;
       }


       pid_t id=fork();
       if(id<0)
       {
           perror("fork");
           return -1;
       }
       else if(id==0)
       {
           execvp(Command_argv[0],Command_argv);
           exit(-1);//replace failed
       }
       else
       {
           int status=0;
           pid_t ret=waitpid(id,&status,0);
           if(ret>0)
           {
               printf("Wating sucess!exit_ret::%d,exit_code::%d,exit_signal::%d\n",ret,(status>>8)&0xFF,status&0x7F);
           }
           else
           {
               printf("Wating failed!\n");
           }
       }

    }
    return 0;
}
